package com.example.xxml_sax;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources.NotFoundException;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;


public class MainActivity extends Activity {
	private TextView textView1;
	boolean tempIsFound;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		textView1 = (TextView) findViewById (R.id.textView1 );
		String content = getXMLContent ();
		textView1.setMovementMethod(ScrollingMovementMethod.getInstance());
	}


	private String getXMLContent() {
		// TODO Auto-generated method stub
		
		StringBuilder sb = new StringBuilder();
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			SAXParser parser = factory.newSAXParser();
			parser.parse(getResources().openRawResource(R.raw.taipeiweather), new DefaultHandler(){
				
				@Override
				public void startDocument() throws SAXException {
					// TODO Auto-generated method stub
					super.startDocument();
					Log.i ("SAX","startDocument");
				}

				
				@Override
				public void startElement(String uri, String localName,
						String qName, Attributes attributes)
						throws SAXException {
					// TODO Auto-generated method stub
					super.startElement(uri, localName, qName, attributes);
					Log.i ("SAX","startElement : "+localName);
					
					if(localName.equals("Temperature")){
						tempIsFound = true;
					}
				}
				
				
				@Override
				public void characters(char[] ch, int start, int length)
						throws SAXException {
					// TODO Auto-generated method stub
					super.characters(ch, start, length);
					String str = new String(ch,start,length);
					Log.i ("SAX","characters : "+str);
					
					if(tempIsFound){
						textView1.setText(str);
					}
				}

				@Override
				public void endDocument() throws SAXException {
					// TODO Auto-generated method stub
					super.endDocument();
					Log.i("SAX","endDocument");
				}

				@Override
				public void endElement(String uri, String localName,
						String qName) throws SAXException {
					// TODO Auto-generated method stub
					super.endElement(uri, localName, qName);
					Log.i ("SAX","endElement : "+localName);
					
					if(localName.equals("Temperature")){
						tempIsFound = false;
					}
				}
				
			});
			
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return sb.toString();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
